package thread.safe;

import java.time.LocalDateTime;
import java.util.concurrent.Semaphore;

/**
 * @author bc
 * @Description:
 * @date 2024/03/11
 */
@SuppressWarnings("ALL")
public class SemaphoreTest {

    public static void main(String[] args) {
        Semaphore semaphore = new Semaphore(3);

        for (int i = 0 ; i < 10; i++) {
            new Thread(() -> {
                try {
                    semaphore.acquire();
                    System.out.println("线程" + Thread.currentThread().getName() + " " + LocalDateTime.now());
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                } finally {
                    semaphore.release();
                }
            }).start();
        }
    }
}
